package com.contory.ssn_lgnd.app.maingame;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;

import android.util.Log;

import com.contory.ssn_lgnd.app.Assets;
import com.contory.ssn_lgnd.app.comp.DrawPtr;
import com.contory.ssn_lgnd.app.comp.UpdatePtr;
import com.contory.ssn_lgnd.framework.Pool;
import com.contory.ssn_lgnd.framework.Pool.PoolObjectFactory;
import com.contory.ssn_lgnd.framework.gl.SpriteBatcher;
import com.contory.ssn_lgnd.framework.math.Vector2;

public class ParticleEffect implements DrawPtr, UpdatePtr{
	Random rand;
	//final Random rand;
	static final byte MAX_PARTICLE_NUM = 30;
	LinkedList<Particle> particleList;
	Pool<Particle> pool;
	Vector2 dir;
	//float strX, strY;
	boolean enableBool = true;
	boolean preEnableBool = true;
	//boolean makeBool = false;
	
	public ParticleEffect(){
		//this.rand = rand;
		particleList = new LinkedList<Particle>();
		PoolObjectFactory<Particle> factory = new PoolObjectFactory<Particle>() {
			@Override
			public Particle createObject() {
				return new Particle(0.1f, 0.1f, Assets.TEX_REG_BUBBLE);
			}
		};
		pool = new Pool<Particle>(factory, MAX_PARTICLE_NUM);
		dir = new Vector2();
	}
	
	public void setRandom(Random rand){
		this.rand = rand;
	}
	
	public void setInit(){
		particleList.clear();
		preEnableBool = true;
		enableBool = true;
	}
	
	//public void setPos(float x, float y){
		//strX = x; strY = y;
	//}


	//public void setMake(){
	//	makeBool = true;
	//}
	
	//public void setUnMake(){
		//makeBool = false;
	//}
	/*
	public void setDir(int dirX){
		if(dirX >= 1)
			dirX = 1;
		else if(dirX <= -1)
			dirX = -1;
		else
			dirX = 1;
		dir.set((float)dirX, 2 * rand.nextFloat() - 1);
		dir.nor();
	}*/
	
	public void makeParticle(int dirX, float strX, float strY){
		if(dirX >= 1)
			dirX = 1;
		else if(dirX <= -1)
			dirX = -1;
		else
			dirX = 1;
		dir.set((float)dirX, 2 * rand.nextFloat() - 1);
		dir.nor();
		Particle ptcChd = pool.newObject();
		ptcChd.init(dir, strX, strY);
		particleList.add(ptcChd);
	}
	
	public void makeParticle(float dirX, float dirY, float strX, float strY){
		dir.set(dirX * rand.nextFloat(), dirY * rand.nextFloat());
		dir.nor();
		Particle ptcChd = pool.newObject();
		ptcChd.init(dir, strX, strY);
		particleList.add(ptcChd);
	}
	
	@Override
	public void update(float deltaTime) {
		if(!enableBool)
			return;
		
		if(particleList.size() == 0 && !preEnableBool){
			enableBool = false;
			return;
		}
		
		for(int i = 0; i < particleList.size(); i++){
			particleList.get(i).update(deltaTime);
		}
		for(int i = 0; i < particleList.size(); i++){
			if(!particleList.get(i).enableBool){
				//Log.d("1", String.valueOf(particleList.get(i).enableBool));
				pool.free(particleList.get(i));
				particleList.remove(particleList.get(i));
			}
		}
	}
	
	public boolean getEnalbeBool(){
		return enableBool;
	}
	
	public void setEnable(){
		preEnableBool = true;
	}
	
	public void setDisable(){
		preEnableBool = false;
	}
	
	@Override
	public void draw(SpriteBatcher batcher) {
		if(!enableBool)
			return;
		for(int i = 0; i < particleList.size(); i++){
			particleList.get(i).draw(batcher);
		}
	}
}
